package com.hk;

import net.slidingmenu.tools.AdManager;
import net.slidingmenu.tools.st.SplashView;
import net.slidingmenu.tools.st.SpotDialogListener;
import net.slidingmenu.tools.st.SpotManager;
import com.hk.activity.MainActivity;
import com.hk.util.Utils;
import com.umeng.onlineconfig.OnlineConfigAgent;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.Window;
import android.view.WindowManager;

/**
 * 启动页
 */
public class AppStart extends Activity {

	Context context;
	SplashView splashView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);

		setContentView(R.layout.welcome);
		context = this;
		OnlineConfigAgent.getInstance().updateOnlineConfig(this);
		AdManager.getInstance(context).init(AppConfig.PUBLISHER_ID,
				AppConfig.Youmi_KEY);

		initAd();
	}

	public void initAd() {

		String first = Utils.getValue(getApplicationContext(), "FirstOpenAPP");
		String showAd = OnlineConfigAgent.getInstance().getConfigParams(this,
				"ShowSplashAd");
		if (first.equals("Yes") && showAd.equals("Yes")) {
			SpotManager.getInstance(context).loadSplashSpotAds();
			SpotManager.getInstance(this).showSplashSpotAds(this,
					MainActivity.class);
		} else {
			Utils.putValue(context, "FirstOpenAPP", "Yes");
			jumpDelay();
		}
	}

	public void jumpDelay() {
		new Handler().postDelayed(new Runnable() {
			@Override
			public void run() {
				jump();
			}
		}, 2000);
	}

	private void jump() {

		startActivity(new Intent(AppStart.this, MainActivity.class));
		finish();
	}

	// 请务必加上词句，否则进入网页广告后无法进去原sdk
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);

		if (resultCode == 10045) {
			Intent intent = new Intent(context, MainActivity.class);
			startActivity(intent);
			finish();
		}
	}

	@Override
	public void onBackPressed() {
		// 取消后退键
	}

	@Override
	protected void onResume() {

		/**
		 * 设置为竖屏
		 */
		if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
			setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		}
		super.onResume();
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
			// land
		} else if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
			// port
		}
	}
}
